//
//  EditOwnerNameViewController.swift
//  ezyform
//
//  Created by Moennig Q on 1/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
class EditOwnerInfomationViewController: BaseViewController {
    private lazy var disposeBag = DisposeBag()
    let customView = EditOwnerInfomationView(topBGImageName: "jobdetail_navi_property_owner_icon")
    var model: PVDDetailModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.model = JobDetailModelHelper.shared.jobDetailModel
        self.initUI()
    }
    func initUI(){
        self.view = self.customView
        self.customView.cancelBtn.isHidden = true
        self.customView.setTitleInfo(R.string.localizable.editOwnerDetails())
        self.customView.continueBtn.setTitle(R.string.localizable.save(), for: UIControl.State.normal)
        self.customView.backToPreBtn.addTarget(self, action: #selector(backToPre), for: UIControl.Event.touchUpInside)
        self.customView.continueClosure = { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.submitOwnerInfo()
        }
        //初始化数据
        let viewModel = self.initViewModel(self.model)
        viewModel.editFlag = true
        self.customView.setData(viewModel)
    }
    //MARK: - 将Model数据的owner info转换为viewModel
    private func initViewModel(_ model: PVDDetailModel)->EditOwnerInfomationViewModel{
        let viewModel = EditOwnerInfomationViewModel()
        viewModel.pvd_id = model.pvd_id
        if let type = PropertyType(rawValue: model.install_address.property_type){
            viewModel.property_type = type
        }
        if let type = OwnerType(rawValue: model.owner_address.type){
            viewModel.owner_type = type
        }
        viewModel.gst = model.gst
        viewModel.email = model.owner_address.email
        viewModel.mobile = model.owner_address.phone
        //individual
        viewModel.first_name = model.owner_address.first_name
        viewModel.surname = model.owner_address.last_name
        viewModel.co_owner_first_name = model.owner_address.co_owner_first_name
        viewModel.co_owner_surname = model.owner_address.co_owner_last_name
        viewModel.co_owner_relation_ship = model.owner_address.co_owner_relation_ship
        //other owner type
        viewModel.company_name = model.owner_address.type_name
        viewModel.home_owner_abn = model.owner_address.home_owner_abn
        viewModel.owner_representative_name = model.owner_address.owner_representative_name
        viewModel.owner_representative_position = model.owner_address.owner_representative_position
        viewModel.price_to_the_home_owner = model.owner_address.price_to_the_home_owner
        viewModel.showCoOwnerFlag = !model.solar_water_heater//热水器不显示co owner，solar panel 才显示
        return viewModel
        
    }
    func submitOwnerInfo(){
        let vm = EditJobInfoService()
        vm.setBlockAfterNetwork { [weak self] (result , _) in
            guard let strongSelf = self else {return}
            ModelViewUtil.shared.hideUpdatingView()
            strongSelf.navigationController?.popViewController(animated: true)
            
        } errorBlock: { (code, error) in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        ModelViewUtil.shared.showLoadingView(R.string.localizable.updating())
        vm.updateOwnerInfo(self.customView.viewModel)

    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
    
}
